<?php
/**
 * 废弃
 * (model)
 */
namespace App\Models\SalesReport;

use App\Models\Basics\SysPlatAccount;
use App\Models\Basics\SysCountry;
use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;
class PaymentPDetail extends Model
{
    protected $table = 'sr_ppayment_detail';

    protected $guarded = [];

    public $timestamps = false;

    public function account(){
        return $this->belongsTo(SysPlatAccount::class,'account_id');
    }

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public static function headersField(){
        $fields = [];
        $fields['report_date'] = ['title' => '日期','sumField' => false];
        $fields['account'] = ['title' => '账号','sumField' => false];
        $fields['area'] = ['title' => '站点','sumField' => false];
        $fields['country'] = ['title' => '国家','sumField' => false];
        $fields['seller'] = ['title' => '销售','sumField' => false];
        $fields['sku'] = ['title' => 'SKU','sumField' => false];
        $fields['sku_cn'] = ['title' => '产品名称','sumField' => false];
        $fields['seller_sku'] = ['title' => '平台SKU','sumField' => false];
        $fields['season'] = ['title' => '季节性','sumField' => false];
        $fields['classify'] = ['title' => '品类','sumField' => false];
        $fields['rate_odds'] = ['title' => '汇率','sumField' => false];
        $fields['sales_volume'] = ['title' => '销量','sumField' => true];
        $fields['sales_amount'] = ['title' => '销售额','sumField' => false];
        $fields['sales_amount_rmb'] = ['title' => '销售额RMB','sumField' => true];
        $fields['selling_fees'] = ['title' => '佣金','sumField' => false];
        $fields['selling_fees_rmb'] = ['title' => '佣金RMB','sumField' => true];
        $fields['fba_fees'] = ['title' => 'FBA交易费','sumField' => false];
        $fields['fba_fees_rmb'] = ['title' => 'FBA交易费RMB','sumField' => true];
        $fields['refund_num'] = ['title' => '退货数量','sumField' => true];
        $fields['refund_amount'] = ['title' => '退款金额','sumField' => false];
        $fields['refund_amount_rmb'] = ['title' => '退款金额RMB','sumField' => true];
        $fields['p_thfba'] = ['title' => '三方发货费','sumField' => false];
        $fields['p_thfba_rmb'] = ['title' => '三方发货费RMB','sumField' => true];
        $fields['purchase_price_rmb'] = ['title' => '采购成本RMB','sumField' => true];
        $fields['t_purchase_price_rmb'] = ['title' => '总采购成本RMB','sumField' => true];
        $fields['shipment_price_rmb'] = ['title' => '运费成本RMB','sumField' => true];
        $fields['t_shipment_price_rmb'] = ['title' => '总运费成本RMB','sumField' => true];
        $fields['ms_fee'] = ['title' => '月仓储费','sumField' => false];
        $fields['ms_fee_rmb'] = ['title' => '月仓储费RMB','sumField' => true];
        $fields['lts_fee'] = ['title' => '长期仓储费','sumField' => false];
        $fields['lts_fee_rmb'] = ['title' => '长期仓储费RMB','sumField' => true];
        $fields['p_icfee'] = ['title' => '库存补偿费','sumField' => false];
        $fields['p_icfee_rmb'] = ['title' => '库存补偿费RMB','sumField' => true];
        $fields['rm_fee'] = ['title' => '移除费用','sumField' => false];
        $fields['rm_fee_rmb'] = ['title' => '移除费用RMB','sumField' => true];
        $fields['rebates'] = ['title' => '促销费','sumField' => false];
        $fields['rebates_rmb'] = ['title' => '促销费RMB','sumField' => true];
        $fields['spend'] = ['title' => '广告费用','sumField' => false];
        $fields['spend_rmb'] = ['title' => '广告费用RMB','sumField' => true];
        $fields['reimburse_rmb'] = ['title' => '刷单费用RMB','sumField' => true];
        $fields['reimburse_os_rmb'] = ['title' => '运营服务费RMB','sumField' => true];
        $fields['reimburse_profit_rmb'] = ['title' => '刷单毛利扣减RMB','sumField' => true];
        $fields['ied_rmb'] = ['title' => '退货二次销售RMB','sumField' => true];
        $fields['rod_rmb'] = ['title' => '销毁货物损失RMB','sumField' => true];
        $fields['vat_fee'] = ['title' => 'VAT费用','sumField' => false];
        $fields['vat_fee_rmb'] = ['title' => 'VAT费用RMB','sumField' => true];
        $fields['profit_rmb'] = ['title' => '毛利值RMB','sumField' => true];
        $fields['profit_rate'] = ['title' => '毛利率','sumField' => false];
        $fields['earning_rmb'] = ['title' => '销售收入RMB','sumField' => true];
        $fields['total'] = ['title' => 'Total','sumField' => false];
        $fields['total_rmb'] = ['title' => 'TotalRMB','sumField' => true];
        $fields['total_rate'] = ['title' => '回款率','sumField' => false];
        return $fields;
    }

    public static function getDownloadData($re){
        foreach ($re as $row){
            $temp = [];
            $temp['report_date'] = $row->report_date;
            $temp['account'] = $row->account->account ?? '';
            $temp['area'] = $row->area->area ?? '';
            $temp['country'] = $row->country->country ?? '';
            $temp['seller'] = User::getUsernameById($row->seller_id);
            $temp['psku_code'] = $row->sku->psku_code ?? '';
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            $temp['seller_sku'] = $row->seller_sku ?: '';
            $temp['season'] = ProductClassify::$_season[$row->sku->classify->season ?? 3];
            $temp['classify'] = $row->sku->classify->title ?? '';
            $temp['rate_odds'] = $row->rate_odds;
            $temp['sales_volume'] = $row->sales_volume;
            $temp['sales_amount'] = $row->sales_amount;
            $temp['sales_amount_rmb'] = $row->sales_amount_rmb;
            $temp['selling_fees'] = $row->selling_fees;
            $temp['selling_fees_rmb'] = $row->selling_fees_rmb;
            $temp['fba_fees'] = $row->fba_fees;
            $temp['fba_fees_rmb'] = $row->fba_fees_rmb;
            $temp['refund_num'] = $row->refund_num;
            $temp['refund_amount'] = $row->refund_amount;
            $temp['refund_amount_rmb'] = $row->refund_amount_rmb;
            $temp['p_thfba'] = $row->p_thfba;
            $temp['p_thfba_rmb'] = $row->p_thfba_rmb;
            $temp['purchase_price_rmb'] = $row->purchase_price_rmb;
            $temp['t_purchase_price_rmb'] = $row->t_purchase_price_rmb;
            $temp['shipment_price_rmb'] = $row->shipment_price_rmb;
            $temp['t_shipment_price_rmb'] = $row->t_shipment_price_rmb;
            $temp['ms_fee'] = $row->ms_fee;
            $temp['ms_fee_rmb'] = $row->ms_fee_rmb;
            $temp['lts_fee'] = $row->lts_fee;
            $temp['lts_fee_rmb'] = $row->lts_fee_rmb;
            $temp['p_icfee'] = $row->p_icfee;
            $temp['p_icfee_rmb'] = $row->p_icfee_rmb;
            $temp['rm_fee'] = $row->rm_fee;
            $temp['rm_fee_rmb'] = $row->rm_fee_rmb;
            $temp['rebates'] = $row->rebates;
            $temp['rebates_rmb'] = $row->rebates_rmb;
            $temp['spend'] = $row->spend;
            $temp['spend_rmb'] = $row->spend_rmb;
            $temp['reimburse_rmb'] = $row->reimburse_rmb;
            $temp['reimburse_os_rmb'] = $row->reimburse_os_rmb;
            $temp['reimburse_profit_rmb'] = $row->reimburse_profit_rmb;
            $temp['ied_rmb'] = $row->ied_rmb;
            $temp['rod_rmb'] = $row->rod_rmb;
            $temp['vat_fee'] = $row->vat_fee;
            $temp['vat_fee_rmb'] = $row->vat_fee_rmb;
            $temp['profit_rmb'] = $row->profit_rmb;
            $temp['profit_rate'] = getPercentage($row->profit_rmb,$row->sales_amount_rmb);
            $temp['total'] = $row->total;
            $temp['total_rmb'] = $row->total_rmb;
            $temp['total_rate'] = getPercentage($row->total_rmb,$row->sales_amount_rmb);
            yield $temp;
        }
    }
}
